﻿using Config.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScreenShoter
{
    public partial class Form1 : Form
    {
        bool realExit = false;

        public Form1()
        {

            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            realExit = true;
            timer1.Stop();
            notifyIcon1.Visible = false;
            Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!realExit)
            {
                e.Cancel = true;
                Opacity = 0;
                ShowInTaskbar = false;
                Hide();
            }
        }
        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
        private void Shot()
        {
            Opacity = 0;

            int width = Screen.PrimaryScreen.Bounds.Width;
            int height = Screen.PrimaryScreen.Bounds.Height;
            Size blockRegionSize = new Size(width, height);

            Bitmap bmp = new Bitmap(width, height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(0, 0, 0, 0, blockRegionSize, CopyPixelOperation.SourceCopy);
            }
            string file = Path.Combine(settings.OutputDir, DateTime.Now.ToString(settings.OutputDatePattern) + ".jpg");

            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);


            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;


            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,
                settings.JpgQuality);
            myEncoderParameters.Param[0] = myEncoderParameter;


            bmp.Save(file, jgpEncoder, myEncoderParameters);
            lblMsg.Text = file;
            Opacity = 100;
        }

        private void btnShot_Click(object sender, EventArgs e)
        {
            btnShot.Enabled = false;
            Shot();
            btnShot.Enabled = true;
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            Opacity = 100;
            ShowInTaskbar = true;
            Show();
            Focus();
        }
        private IShotSettings settings;

        private void Form1_Load(object sender, EventArgs e)
        {
            settings = new ConfigurationBuilder<IShotSettings>()
             .UseIniFile("shot.ini")
             .Build();
            if (!Directory.Exists(settings.OutputDir))
                Directory.CreateDirectory(settings.OutputDir);
            timer1.Interval = settings.ShotInterval * 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Shot();
        }
    }
}
